package omm.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import omm.domain.User;
import omm.domain.evolutiongraph.ClientsQuantityUnitStrategy;
import omm.domain.exceptions.DatabaseException;
import omm.domain.exceptions.UserCannotLoginException;
import omm.domain.exceptions.UserWrongPasswordException;
import omm.services.UserServices;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

	private UserServices userServices;
	private static final Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	public UserController(UserServices userServices) {
		this.userServices = userServices;
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView login(HttpSession session,
			HttpServletRequest servletrequest,
			HttpServletResponse resp) {

		ModelAndView mav = new ModelAndView();

		String username="";
		try {
			username = ServletRequestUtils.getStringParameter(
					servletrequest, "username");
			String passwordMD5 = ServletRequestUtils.getStringParameter(
					servletrequest, "passwd");

			//mav.addObject("username", username);
			//mav.addObject("passwd", passwordMD5);

			User user = new User(username, passwordMD5);
			user = userServices.authenticate(user);

			session.setAttribute("user", user);
			mav.addObject("error", '0');
			logger.info("Se logueo al usuario: "+username);
		} catch (UserCannotLoginException baduser) {
			mav.addObject("error", '1');
			session.setAttribute("user", null);
			logger.info("No se pudo loguear al usuario: "+username,baduser);
		} catch (UserWrongPasswordException wrongpass) {
			mav.addObject("error", '1');
			session.setAttribute("user", null);
			logger.info("No se pudo loguear al usuario: "+username,wrongpass);
		} catch (ServletRequestBindingException e) {
			mav.addObject("error", '2');
			session.setAttribute("user", null);
			logger.info("No se pudo loguear al usuario: "+username,e);
		} catch (DatabaseException dbe) {
			mav.addObject("error", '2');
			session.setAttribute("user", null);
			logger.info("No se pudo loguear al usuario: "+username,dbe);
		} catch (Exception e) {
			mav.addObject("error", '2');
			session.setAttribute("user", null);
			logger.info("No se pudo loguear al usuario: "+username,e);
		}
	
		return mav;
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView logout(HttpSession session) {
		ModelAndView mav = new ModelAndView();
		try {
			session.invalidate();
			mav.addObject("error", '0');
			logger.info("Se logueo el usuario");
		} catch (IllegalStateException e) {
			mav.addObject("error", '1'); // Ya se habia deslogueado 
		}
		return mav;
	}

}
